package com.JSXW.demo;

public class StudentText {
    public static void main(String[] args) {
        Student[] studentArr = new Student[3];
        Student student1 = new Student(001,"李",19);
        Student student2 = new Student(002,"张",20);
        Student student3 = new Student(003,"刘",21);
        studentArr[0] = student1;
        studentArr[1] = student2;
        studentArr[2] = student3;
        Student[] studentArr2 = new Student[studentArr.length+1];
        //System.arraycopy数组复制,原数组,起始索引,目标数组,目标数组起始索引,复制的长度
        System.arraycopy(studentArr, 0, studentArr2, 0, studentArr.length);
//        Student student4 = new Student(001,"王",22);
        Student student4 = new Student(001,"王",22);

        boolean flag = addList(studentArr, student4);
        if (!flag) {
            studentArr2[studentArr2.length-1] = student4;
        }
        studentArr = studentArr2;
        detList(studentArr, 001);
        showAdd(studentArr, 003);
        for (int i = 0; i < studentArr.length; i++) {
            if (studentArr[i] != null) {
                System.out.println(studentArr[i].getAge());
            }
        }
    }
    public static boolean addList (Student[] val, Student old){
        boolean flag = false;
        for (int i = 0; i < val.length; i++) {
            if (val[i] != null && (val[i].getId() == old.getId())) {
                flag = true;
                System.out.println("重复");
            }
        }
        return  flag;
    }
    public static void detList (Student[] val, int id){
        for (int i = 0; i < val.length; i++) {
            if ( val[i] != null && val[i].getId() == id) {
                val[i] = null;
                System.out.println("删除成功");
            }
        }
    }
    public static void showAdd(Student[] val, int id){
        for (int i = 0; i < val.length; i++) {
            if ( val[i] != null && val[i].getId() == id) {
                val[i].setAge(val[i].getAge()+1);
            }
        }
    }
}
